package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("user/{id}/{name}")
    public String user(Model model, @PathVariable int id,@PathVariable String name){
        model.addAttribute("name","哈哈");
        model.addAttribute("date",new Date());
        model.addAttribute("address","北京市海淀区");
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        model.addAttribute("list",strings);
        return "index";
    }
    @RequestMapping("findAll")
    public String findAll(Model model){
        List<User> list=userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }
    @RequestMapping("add")
    public String add(User user){
        userService.add(user);
        return "redirect:/user/findAll";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Integer id){
        userService.delete(id);
        return "redirect:/user/findAll";
    }
    @RequestMapping("toEdit/{id}")
    public String toEdit(@PathVariable Integer id,Model model){
        User user=userService.findByid(id);
        model.addAttribute("user",user);
        return "edit";
    }
    @RequestMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/findAll";
    }

}
